<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增商品')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: bootstrap-select-css"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: summernote-css"/>
    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: ztree-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-product-add">
        <input name="tempId" type="hidden">
        <div class="form-group">
            <label class="col-sm-2 control-label "><span style="color: red; ">*</span>所属分类：</label>
            <div class="col-sm-8">
                <div id="categoryTrees" class="ztree"></div>
            </div>
        </div>
        <!--        <div class="form-group">-->
        <!--            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>规格分类：</label>-->
        <!--            <div class="col-sm-3">-->
        <!--                <select name="attrCategoryId" th:with="cateList=${attrCategoryList}" class="form-control">-->
        <!--                    <option th:each="cate:${cateList}" th:value="${cate.id}" th:text="${cate.categoryName}"></option>-->
        <!--                </select>-->
        <!--            </div>-->
        <!--        </div>-->
        <div class="form-group">
            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>商品名称：</label>
            <div class="col-sm-5">
                <input class="form-control" type="text" name="productName" id="productName" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">封面图：</label>
            <div class="col-sm-5">
                <input class="form-control" type="file" id="imageBtn" name="imageBtn" onchange="uploadImage(2)"/>
                <input class="form-control" name="coverUrl" id="imageUrl" type="hidden">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">多图：</label>
            <div class="col-sm-6 table-striped">
                <table id="multi-picture-table"></table>
            </div>
        </div>
        <!--<div class="form-group">-->
            <!--<label class="col-sm-2 control-label"><span style="color: red; ">*</span>重量：</label>-->
            <!--<div class="col-sm-3">-->
                <!--<input class="form-control" type="number" name="weight" required>-->
            <!--</div>-->
        <!--</div>-->
        <!--<div class="form-group">-->
            <!--<label class="col-sm-2 control-label"><span style="color: red; ">*</span>成本价：</label>-->
            <!--<div class="col-sm-3">-->
                <!--<input class="form-control" type="number" placeholder="必须填写，店铺进货以此价格付款" name="buyPrice" id="buyPrice"-->
                       <!--required>-->
            <!--</div>-->
        <!--</div>-->
        <div class="form-group">
            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>购买单位：</label>
            <div class="col-sm-3">
                <select class="form-control" name="buyUnit">
                    <option value="个">个</option>
                    <option value="支">支</option>
                    <option value="箱">箱</option>
                    <option value="罐">罐</option>
                    <option value="件">件</option>
                    <option value="盒">盒</option>
                    <option value="包">包</option>
                    <option value="套">套</option>
                    <option value="袋">袋</option>
                    <option value="瓶">瓶</option>
                    <option value="条">条</option>
                    <option value="排">排</option>
                    <option value="提">提</option>
                    <option value="组">组</option>
                    <option value="桶">桶</option>
                    <option value="筒">筒</option>
                    <option value="杯">杯</option>
                    <option value="碗">碗</option>
                    <option value="斤">斤</option>
                    <option value="粒">粒</option>
                    <option value="元">元</option>
                    <option value="份">份</option>
                    <option value="只">只</option>
                    <option value="根">根</option>
                    <option value="副">副</option>
                    <option value="托">托</option>
                    <option value="串">串</option>
                    <option value="部">部</option>
                    <option value="台">台</option>
                </select>
                <!--                <input class="form-control" type="text" placeholder="购买单位" name="buyUnit" value="个"-->
                <!--                       required>-->
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">备注：</label>
            <div class="col-sm-4">
                <input class="form-control" type="text" name="appendRemark" maxlength="24" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"></label>
            <div class="col-sm-4">
                <span class="help-block m-b-none" style="color: red; "><i class="fa fa-info-circle" ></i>建议11个中文字符内，效果最佳</span>
            </div>
        </div>
        <!--<div class="form-group">-->
            <!--<label class="col-sm-2 control-label"><span style="color: red; ">*</span>总库存：</label>-->
            <!--<div class="col-sm-3">-->
                <!--<input class="form-control" type="number" name="stock" required>-->
            <!--</div>-->
        <!--</div>-->
        <div class="form-group">
            <label class="col-sm-2 control-label ">sku列表：</label>
            <div class="col-sm-3">
                <div class="input-group">
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-success" onclick="addSku()">新增SKU</button>
                    </span>
                </div>
<!--                <div class="input-group">-->
<!--                    <input type="number" class="form-control" placeholder="成本价" id="skuBuyPrice"/>-->
<!--                    <span class="input-group-btn">-->
<!--                        <button type="button" class="btn-xs btn-success" onclick="batchConfig(1)">批量修改</button>-->
<!--                    </span>-->
<!--                </div>-->
<!--                <div class="input-group">-->
<!--                    <input type="number" class="form-control" placeholder="建议售价" id="skuPrice"/>-->
<!--                    <span class="input-group-btn">-->
<!--                        <button type="button" class="btn-xs btn-success" onclick="batchConfig(2)">批量修改</button>-->
<!--                    </span>-->
<!--                </div>-->
<!--                <div class="input-group">-->
<!--                    <input type="number" class="form-control" placeholder="库存" id="skuStock"/>-->
<!--                    <span class="input-group-btn">-->
<!--                        <button type="button" class="btn-xs btn-success" onclick="batchConfig(3)">批量修改</button>-->
<!--                    </span>-->
<!--                </div>-->
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"></label>
            <div class="col-sm-9 table-striped">
                <table id="sku-table"></table>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>商品详情：</label>
            <div class="col-sm-7">
                <input id="memo" name="memo" type="hidden">
                <div class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>显示排序：</label>
            <div class="col-sm-3">
                <input class="form-control" type="number" name="orderNum" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label"><span style="color: red; ">*</span>状态：</label>
            <div class="col-sm-3">
                <div class="radio-box" th:each="dict : ${@dict.getType('show_status')}">
                    <input type="radio" th:id="${dict.dictCode}" name="isEnable" th:value="${dict.dictValue}"
                           th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <h4 class="form-header h4"></h4>
        <div class="row">
            <div class="col-sm-offset-2 col-sm-2">
                <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                        class="fa fa-check"></i>保 存
                </button>
                </button>&nbsp;
                <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i
                        class="fa fa-reply-all"></i>关 闭
                </button>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<th:block th:include="include :: summernote-js"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: bootstrap-select-js"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<th:block th:include="include :: ztree-js"/>

<script type="text/javascript">

    $('input[name="tempId"]').val($.common.random(100000000, 999999999));

    var prefix = ctx + "back/product";
    $(function () {
        var url = ctx + "back/productCategory/treeData";
        var options = {
            id: "categoryTrees",
            url: url,
            check: {enable: true},
            expandLevel: 0
        };
        $.tree.init(options);

        var options2 = {
            url: ctx + "back/commonImage/searchList",
            id: "multi-picture-table",
            modalName: "设置商品多图",
            uniqueId: "id",
            showSearch: false,
            showRefresh: false,
            pagination: false,
            showToggle: false,
            showColumns: false,
            clickToSelect: true,
            rememberSelected: true,
            queryParams: queryParams,
            onEditableSave: onMutiPictureEditableSave,
            onLoadSuccess: function (data) {
                for (let i = 0; i < 5 - data.total; i++) {
                    $("#multi-picture-table").bootstrapTable('insertRow', {
                        index: data.total, // 你想插入到哪，0表示第一行
                        row: {
                            coverUrl: "",
                            isEnable: 1,
                            orderNum: 1
                        }
                    });
                }
                $('.product-img-upload').change(function () {
                    if ($(this).val() != '') {
                        var formData = new FormData();
                        formData.append('coverFile', $(this)[0].files[0]);
                        formData.append('modelType', 1);
                        formData.append('modelId', $('input[name="tempId"]').val());
                        $.ajax({
                            url: ctx + "back/commonImage/add",
                            type: 'POST',
                            data: formData,
                            async: false,
                            success: function (res) {
                                $.modal.alertSuccess("上传成功！")
                                $('#multi-picture-table').bootstrapTable('refresh');
                            },
                            error: function () {
                                $('#multi-picture-table').bootstrapTable('refresh');
                            },
                            cache: false,
                            contentType: false,
                            processData: false
                        });
                    }
                });
            },
            columns: [
                {
                    field: 'id',
                    visible: false
                },
                {
                    field: 'coverUrl',
                    title: '图片',
                    formatter: function (value, row, index) {
                        var actions = [];
                        let btnName = "上传";
                        if (parseInt(row.id) !== 0) {
                            if ($.common.isNotEmpty(value)) {
                                var imgStr = $.common.sprintf("<img class='img-circle img-sm' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>", 400, 'auto', 'self', value, value);
                                actions.push(imgStr);
                                btnName = "更换";
                            } else {
                                actions.push('<div class="fileinput fileinput-new" data-provides="fileinput"><span class="btn btn-success btn-file btn-xs"><span class="fileinput-new">' + btnName + '</span><input class="product-img-upload" data-id="' + row.skuCode + '" type="file"</span></div>');
                            }
                        } else {
                            actions.push('');
                        }
                        return actions.join('');
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    align: 'center',
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "正常"
                        }, {
                            value: 0,
                            text: "停用"
                        }]
                    }
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    align: 'center',
                    editable: {
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[1-9]\d*$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                }, {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (row.coverUrl != "") {
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeImage(\'' + row.id + '\')"><i class="fa fa-remove">删除</i></a> ');
                        }
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options2);
        var options3 = {
            id: "sku-table",
            modalName: "设置商品Sku",
            uniqueId: "skuCode",
            showSearch: false,
            showRefresh: false,
            pagination: false,
            showToggle: false,
            showColumns: false,
            rememberSelected: true,
            onEditableSave:onSkuEditableSave,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    visible: false,
                },
                {
                    field: 'skuCode',
                    title: '编号',
                    editable: {
                        type: 'text',
                        title: '编号',
                        emptytext: "【编号】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【编号】不能为空!';
                            }
                        }
                    }

                },
                {
                    field: 'coverUrl',
                    title: 'sku封面图',
                    formatter: function (value, row, index) {
                        var actions = [];
                        let btnName = "上传";
                        if (value !== "") {
                            if ($.common.isNotEmpty(value)) {
                                var imgStr = $.common.sprintf("<img class='img-circle img-xs' data-height='%s' data-width='%s' data-target='%s'  style='border-radius: 0;' src='%s' data-src='%s'/>", 400, 'auto', 'self', value, value);
                                actions.push(imgStr);
                                actions.push('<span class="btn btn-success btn-file btn-xs"><span class="fileinput-new">更换</span><input onchange="uploadSkuImage(this)" class="sku-img-upload" data-id="' + row.skuCode + '" type="file"</span>');
                            } else {
                                actions.push('<div class="ml5 fileinput fileinput-new" data-provides="fileinput"><span class="btn btn-success btn-file btn-xs"><span class="fileinput-new">' + btnName + '</span><input onchange="uploadSkuImage(this)" class="sku-img-upload" data-id="' + row.skuCode + '" type="file"</span></div>');
                            }
                        } else {
                            actions.push('');
                        }
                        return actions.join('');
                    }
                },

                {
                    field: 'skuJson',
                    title: '规格值',
                    editable: {
                        type: 'text',
                        title: '规格值',
                        emptytext: "必填",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【规格值】不能为空!';
                            }
                        }
                    }
                },
                {
                    field: 'buyPrice',
                    title: '成本价',
                    editable: {
                        type: 'text',
                        title: '建议卖价',
                        emptytext: "必填",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【建议卖价】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【建议卖价】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【建议卖价】';
                            }
                        }
                    }
                },
                {
                    field: 'originPrice',
                    title: '原价',
                    editable: {
                        type: 'text',
                        title: '原价',
                        emptytext: "0",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【原价】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【原价】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【原价】';
                            }
                        }
                    }
                },
                {
                    field: 'isPromotion',
                    title: '是否促销',
                    editable: {
                        type: 'select',
                        title: '是否促销',
                        source: [{
                            value: 1,
                            text: "是"
                        }, {
                            value: 0,
                            text: "否"
                        }]
                    }
                },
                {
                    field: 'price',
                    title: '建议卖价',
                    editable: {
                        type: 'text',
                        title: '建议卖价',
                        emptytext: "必填",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【建议卖价】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【建议卖价】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【建议卖价】';
                            }
                        }
                    }
                },
                {
                    field: 'weight',
                    title: '重量（g）',
                    editable: {
                        type: 'text',
                        title: '重量',
                        emptytext: "必填",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【重量】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【重量】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【重量】';
                            }
                        }
                    }
                },
                {
                    field: 'stock',
                    title: '库存',
                    editable: {
                        type: 'text',
                        title: '库存',
                        emptytext: "必填",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【库存】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【库存】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【库存】';
                            }
                        }
                    }
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    editable: {
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    align: 'center',
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "正常"
                        }, {
                            value: 0,
                            text: "停用"
                        }]
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeSku(\'' + row.skuCode + '\')"><i class="fa fa-remove">删除</i></a> ');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options3);
    });

    function batchConfig(type) {
        const data = $("#sku-table").bootstrapTable('getSelections');
        if (data.length === 0) {
            $.modal.alertError("请先勾选记录！");
            return;
        }
        let row;
        switch (type) {
            case 1:
                row = {buyPrice: $('#skuBuyPrice').val()};
                break;
            case 2:
                row = {price: $('#skuPrice').val()};
                break;
            case 3:
                row = {stock: $('#skuStock').val()};
                break;
            default:
                $.modal.alertError("批量修改失败！");
                return;
        }
        for (let i = 0; i < data.length; i++) {
            $("#sku-table").bootstrapTable('updateByUniqueId', {
                id: data[i].skuCode,
                row: row
            });
        }
        $.modal.alertSuccess("批量修改成功！");
    }

    function addSku() {
        const skuCode = $.common.random(100000000000, 999999999999)
        $("#sku-table").bootstrapTable('insertRow', {
            index: 0, // 你想插入到哪，0表示第一行
            row: {
                id: 0,
                skuCode: skuCode,
                skuJson: "",
                buyPrice: null,
                isPromotion: 0,
                originPrice: 0,
                price: null,
                isEnable: 1,
                weight:null,
                stock: null,
                orderNum: 0
            }
        });
    }

    function queryParams2(params) {
        var search = $.table.queryParams(params);
        search.productId = $("#productId").val();
        search.searchType = $("#searchType").val();
        return search;
    }

    function uploadSkuImage(that) {
        if ($(that).val() != '') {
            const skuCode = $(that).attr("data-id");
            ajaxUploadFile(function (result) {
                if (result.code == web_status.SUCCESS) {
                    $("#sku-table").bootstrapTable('updateByUniqueId', {
                        id: skuCode,
                        row: {
                            coverUrl: result.url,
                        }
                    });
                    $.modal.alertSuccess("上传成功！")
                } else {
                    $.modal.alertError(result.msg);
                }
            }, $(that)[0].files[0], 3)
        }
    }

    function removeSku(skuCode) {
        $.modal.confirm("是否删除所选规格值？", function () {
            $("#sku-table").bootstrapTable('removeByUniqueId', skuCode);
        })
    }

    function queryParams(params) {
        var search = $.table.queryParams(params);
        search.modelId = $('input[name="tempId"]').val();
        return search;
    }

    // 商品详情富文本初始化
    $('.summernote').summernote({
        placeholder: '请填写商品详情',
        height: 300,
        lang: 'zh-CN',
        followingToolbar: false,
        callbacks: {
            //上传图片事件
            onImageUpload: function (files) {
                ajaxUploadFile(function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $('.summernote').summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                }, files[0], 3)
            }
        }
    });


    $("#form-product-add").validate({
        onkeyup: false,
        rules: {
            productName: {
                remote: {
                    url: prefix + "/checkProductNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "productName": function () {
                            return $.common.trim($("#productName").val());
                        },
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            }
        },
        messages: {
            "productName": {
                remote: "商品已存在，请勿重复添加"
            }
        },
        focusCleanup: true
    });

    //上传图片文件
    $("#imageBtn").fileinput({
        language: 'zh', //设置语言
        dropZoneTitle: '拖拽文件到这里 &hellip;<br>仅支持上传一张图片<br>多次上传替换前一张图片',
        allowedFileExtensions: ['jpg', 'png', 'bmp', 'jpeg'],//接收的文件后缀
        showUpload: false, //是否上传文件
        showRemove: true,
        dropZoneEnabled: true,//是否显示拖拽区域
        maxFileCount: 1,
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
    }).on('fileclear', function (event, id) {
        $.modal.alertSuccess("删除图片成功！")
        $("#imageUrl").val('');
    });

    //通用上传文件
    function ajaxUploadFile(callback, file, modelType) {
        var formdata = new FormData();
        formdata.append("file", file);
        formdata.append("modelType", modelType);
        $.modal.loading("上传中...")
        $.ajax({
            url: ctx + "back/upload/image",
            data: formdata,
            type: "post",
            processData: false,
            contentType: false,
            success: function (result) {
                $.modal.closeLoading();
                callback(result);
            },
            error: function () {
                $.modal.closeLoading();
                $.modal.alertWarning("图片上传失败");
            }
        })
    }

    //图片文件上传
    function uploadImage(modelType) {
        var files = document.getElementById('imageBtn');
        if (files) {
            ajaxUploadFile(function (result) {
                if (result.code == 0) {
                    $.modal.alertSuccess("上传图片成功！")
                } else {
                    $.modal.alertError("上传图片失败，请重试");
                }
                $("#imageUrl").val(result.url);
            }, files.files[0], modelType);
        }
    }

    function removeImage(imageId) {
        $.modal.confirm("是否删除该封面图？", function () {
            var formdata = new FormData();
            formdata.append("ids", imageId);
            $.ajax({
                url: ctx + "back/commonImage/remove",
                type: 'POST',
                data: formdata,
                async: false,
                success: function (res) {
                    $.modal.alertSuccess("删除成功！")
                    $('#multi-picture-table').bootstrapTable('refresh');
                },
                error: function () {
                    $('#multi-picture-table').bootstrapTable('refresh');
                },
                cache: false,
                contentType: false,
                processData: false
            });
        });
    }

    function onMutiPictureEditableSave(field, row, oldValue, $el) {
        if (row.id == null) {
            $.modal.alertError("请先上传图片再修改！");
            $('#multi-picture-table').bootstrapTable('refresh');
            return false;
        }
        let updateData = {
            id: row.id,
            orderNum: row.orderNum,
            isEnable: row.isEnable,
        };
        $.ajax({
            type: 'post',
            url: ctx + "back/commonImage/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                    $('#multi-picture-table').bootstrapTable('refresh');
                }
            }
        });
    }

    function onSkuEditableSave(field, row, oldValue, $el) {
        if(field == "skuCode" && row.skuCode!=oldValue){
            $('.sku-img-upload').each(function(){
                if($(this).attr("data-id") == oldValue){
                    $(this).attr("data-id",row.skuCode)
                }
            });
        }

        if(field == "originPrice" || field == "isPromotion"){
            var initVal = $el.html();
            var originPrice = parseFloat(row.originPrice);
            var buyPrice = parseFloat(row.buyPrice);
            if(originPrice<=buyPrice &&  parseInt(row.isPromotion) == 1){
                $.modal.alertError("原价必须大于成本价！");
                if(field == "isPromotion"){
                    row.isPromotion = $el.val();
                }else{
                    row.originPrice = $el.html();
                }
                $('#sku-table').bootstrapTable('updateRow', {index: row.rowId, row: row});
                return;
            }
        }
    }

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#memo").val(sHTML);
            let productObj = $("#sku-table").bootstrapTable('getData');
            let data = $('#form-product-add').serializeArray();
            data.push({"name": "categoryIds", "value": $.tree.getCheckedNodes()});
            data.push({"name": "sku", "value": JSON.stringify(productObj)});
            // console.log(data)
            $.operate.saveTab(prefix + "/add", data);
        }
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
</script>
</body>
</html>
